

local DIY = require 'packages.diy_utility.diy_utility'

local chubu = fk.CreateSkill {
  name = "inker_chubu",
  tags = { Skill.Compulsory },
  dynamic_desc = function(self, player)
    if player and DIY.getSwitchState(player, "inker_chubu") ~= 0 then
      local state = DIY.getSwitchState(player, "inker_chubu")
      return "inker_chubu_dynamic" .. state
    else
      return "inker_chubu"
    end
  end
}

chubu:addAcquireEffect(function (self, player, is_start)
  DIY.setSwitchState(player, chubu.name, 1, 3)
  if is_start then return end
  player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
    local data = e.data
    if data.from == player and data.card then
      player.room:setPlayerMark(player, "@inker_chubu_number", data.card.number)
      return true
    end
  end, 0)
end)
chubu:addLoseEffect(function (self, player, is_death)
  DIY.removeSwitchSkill(player, chubu.name)
  player.room:setPlayerMark(player, "@inker_chubu_number", 0)
  player.room:setPlayerMark(player, "@inker_chubu_chu", 0)
end)

chubu:addEffect(fk.CardUseFinished, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(chubu.name) and data.extra_data and data.extra_data.inker_chubu_decreased
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local state = DIY.getSwitchState(player, chubu.name)
    DIY.changeSwitchState(player, chubu.name)
    if state == 1 then
      room:addPlayerMark(player, "@inker_chubu_chu")
      return
    end
    local card_ids = player:getCardIds("h")
    if #card_ids > 0 then
      room:moveCards({
        ids = player:getCardIds("h"),
        from = player,
        toArea = Card.DiscardPile,
        skillName = chubu.name,
        moveReason = fk.ReasonPutIntoDiscardPile,
        proposer = player,
      })
      room:sendFootnote(card_ids, {
        type = "##InkerMeltCard",
        from = player.id,
      })
      room:broadcastPlaySound("./audio/system/fire_damage")
      room:sendLog{
        type = "#InkerMeltBySkill",
        from = player.id,
        card = card_ids,
        arg = chubu.name,
      }
      if player.dead then return end
      player:drawCards(1, chubu.name)
    end
    if state == 2 then return end

    room:changeMaxHp(player, -1)
  end
})
chubu:addEffect(fk.CardUsing, {
  mute = true,
  can_refresh = function(self, event, target, player, data)
    if target == player and player:hasSkill(chubu.name, true) then
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getMark("@inker_chubu_number")
    local num = data.card.number
    if mark > 0 and num > 0 and mark > num then
      data.extra_data = data.extra_data or {}
      data.extra_data.inker_chubu_decreased = true
    end
    player.room:setPlayerMark(player, "@inker_chubu_number", data.card.number)
  end,
})

Fk:loadTranslationTable{
  ["inker_chubu"] = "躇步",
  [":inker_chubu"] = "转换技，锁定技，当你使用牌结算后，①②③若与你上一使用牌点数递减，②③则<a href=':inker_melt'>熔铸</a>所有手牌③并减1点体力上限。<br>" ..
  "<a href=':programmer_inker_chubu'>实际上……？</a>",

  [":inker_chubu_dynamic1"] = "转换技，锁定技，当你使用牌结算后，<font color=\"#E0DB2F\">①②③若与你上一使用牌点数递减，则获得一枚“孤”，</font>②③<a href=':inker_melt'>熔铸</a>所有手牌③并减1点体力上限。<br>" ..
  "<a href=':programmer_inker_chubu1'>实际上……？</a>",
  [":inker_chubu_dynamic2"] = "转换技，锁定技，当你使用牌结算后，<font color=\"#E0DB2F\">①②③若与你上一使用牌点数递减，②③则<a href=':inker_melt'>熔铸</a>所有手牌</font>③并减1点体力上限。<br>" ..
  "<a href=':programmer_inker_chubu2'>实际上……？</a>",
  [":inker_chubu_dynamic3"] = "转换技，锁定技，当你使用牌结算后，<font color=\"#E0DB2F\">①②③若与你上一使用牌点数递减，②③则<a href=':inker_melt'>熔铸</a>所有手牌③并减1点体力上限。</font><br>" ..
  "<a href=':programmer_inker_chubu3'>实际上……？</a>",

  [":inker_melt"] = "将一至多张牌置入弃牌堆，摸一张牌。",
  [":programmer_inker_chubu"] = "转换技，锁定技，当你使用牌结算后，若与你上一使用牌点数递减，你依次执行前序号项：<br>" ..
  "①获得一枚“躇”；<br>" ..
  "②将所有手牌置入弃牌堆，摸一张牌；<br>" ..
  "③减1点体力上限。",
  [":programmer_inker_chubu1"] = "转换技，锁定技，当你使用牌结算后，若与你上一使用牌点数递减，你依次执行前一项：<br><font color=\"#E0DB2F\">" ..
  "①获得一枚“躇”；</font><br>" ..
  "②将所有手牌置入弃牌堆，摸一张牌；<br>" ..
  "③减1点体力上限。",
  [":programmer_inker_chubu2"] = "转换技，锁定技，当你使用牌结算后，若与你上一使用牌点数递减，你依次执行前两项：<br><font color=\"#E0DB2F\">" ..
  "<s>①获得一枚“躇”；</s><br>" ..
  "②将所有手牌置入弃牌堆，摸一张牌；</font><br>" ..
  "③减1点体力上限。",
  [":programmer_inker_chubu3"] = "转换技，锁定技，当你使用牌结算后，若与你上一使用牌点数递减，你依次执行前三项：<br><font color=\"#E0DB2F\">" ..
  "<s>①获得一枚“躇”；</s><br>" ..
  "②将所有手牌置入弃牌堆，摸一张牌；<br>" ..
  "③减1点体力上限。</font>",

  ["##InkerMeltCard"] = "%from熔铸",
  ["#InkerMeltBySkill"] = "%from 因 “%arg” 熔铸了 %card",

  ["@inker_chubu_number"] = "躇步",
  ["@inker_chubu_chu"] = "躇",
}

return chubu
